package com.ulic.customerManApplication.studentmodule.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ulic.customerManApplication.base.BaseAction;
import com.ulic.customerManApplication.commonmodule.bo.Organization;
import com.ulic.customerManApplication.constant.CONSTANT;
import com.ulic.customerManApplication.pager.PagerModel;
import com.ulic.customerManApplication.studentmodule.bo.Student;
import com.ulic.customerManApplication.studentmodule.service.StudentService;
import com.ulic.customerManApplication.studentmodule.vo.StudentVO;
import com.ulic.customerManApplication.usermodule.bo.User;
import com.ulic.customerManApplication.util.excel.DownloadExcel;
import com.ulic.customerManApplication.util.excel.ExpStudents;
@Component("studentAction")
@Scope("prototype")
public class StudentAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
    private StudentService studentService;
    private Student student;
    private String startcreate;
    private String endcreate;
    private String subCityHidden;
	
	public String getSubCityHidden() {
		return subCityHidden;
	}
	public void setSubCityHidden(String subCityHidden) {
		this.subCityHidden = subCityHidden;
	}
	public String getStartcreate() {
		return startcreate;
	}
	public void setStartcreate(String startcreate) {
		this.startcreate = startcreate;
	}
	public String getEndcreate() {
		return endcreate;
	}
	public void setEndcreate(String endcreate) {
		this.endcreate = endcreate;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	public StudentService getStudentService() {
		return studentService;
	}
	@Resource
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		HttpServletRequest httpServletRequest = ServletActionContext. getRequest();
		PagerModel pm = studentService.getStudents(curPage);
		httpServletRequest.setAttribute("pm",pm);
		return SUCCESS;
	}
	
	public String searchStudents(){
		
		HttpServletRequest httpServletRequest = ServletActionContext. getRequest();
		String buttonName = httpServletRequest.getParameter("buttonType");
		if(!"exp".equals(buttonName)){
		
		String roleName = (String)session.get("rolename");
		User user = (User)session.get(CONSTANT.USER_SESSION_KEY);
		StudentVO studentVO = new StudentVO();
		studentVO.setRoleName(roleName);
		if(student==null){
			student = new Student();
		}
		if("SUBADMIN".equals(roleName)){
			 Organization organ = (Organization)session.get("barchList");
			 if(organ!=null){
				 student.setSubCompany(organ.getUm_organ_id());
			 }
		}else if("THIRDADMIN".equals(roleName)){
			Organization subComorgan = (Organization)session.get("barchList");
			 if(subComorgan!=null){
				 student.setSubCompany(subComorgan.getUm_organ_id());
			 }
			Organization organ = (Organization)session.get("expensedList");
			 if(organ!=null){
				 student.setSubCity(organ.getUm_organ_id());
			 }
		} 
		studentVO.setStudent(student);
		studentVO.setUser(user);
		PagerModel pm = studentService.getStudents(studentVO,curPage);
		httpServletRequest.setAttribute("pm",pm);
		httpServletRequest.setAttribute("roleName", roleName);
		return SUCCESS;
		}else{
			return expStudents();
		}
	}
	
	 
	public String viewStudent()throws Exception{
		student = studentService.getStudent(student.getId());
		return SUCCESS;
	}
	public String updateStudent()throws Exception{
		student =studentService.getStudent(student.getId());
		return SUCCESS;
	}
	public String updatedStudent()throws Exception{
		 User user = (User)session.get(CONSTANT.USER_SESSION_KEY);
		 student.setUser(user);
		 studentService.modifyStudent(student);
		 return SUCCESS;
	}
	public String deleteStudent()throws Exception{
		student = studentService.getStudent(student.getId());
		studentService.removeStudent(student);
		return SUCCESS;
	}
	 
	public String expStudents(){
		HttpServletRequest httpServletRequest = ServletActionContext. getRequest();
		HttpServletResponse httpServletResponse = ServletActionContext.getResponse();
		String roleName = (String)session.get("rolename");
		User user = (User)session.get(CONSTANT.USER_SESSION_KEY);
		StudentVO studentVO = new StudentVO();
		studentVO.setRoleName(roleName);
		if(student==null){
			student = new Student();
		}
		if("SUBADMIN".equals(roleName)){
			 Organization organ = (Organization)session.get("barchList");
			 if(organ!=null){
				 student.setSubCompany(organ.getUm_organ_id());
			 }
		}else if("THIRDADMIN".equals(roleName)){
			Organization subComorgan = (Organization)session.get("barchList");
			 if(subComorgan!=null){
				 student.setSubCompany(subComorgan.getUm_organ_id());
			 }
			Organization organ = (Organization)session.get("expensedList");
			 if(organ!=null){
				 student.setSubCity(organ.getUm_organ_id());
			 }
		} 
		studentVO.setStudent(student);
		studentVO.setUser(user);
		PagerModel pm = studentService.getStudents(studentVO,curPage);
		String realPath = httpServletRequest.getRealPath("/download/");
		String fileName = "学生信息.xls";
		String[] headers = {"分公司","中支","姓名","性别","学校"}; 
		ExpStudents.exp(headers,studentService.getStudents(studentVO,1,pm.getTotal()==0?1:pm.getTotal()).getDatas(),realPath+"/"+fileName);//将数据读取到服务器上的文件中
	    DownloadExcel.download(realPath,fileName,httpServletRequest,httpServletResponse);//从服务器上下载文件
		return null;
	}
	
}
